<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class mtag extends MY_Model {

    var $theme_data = array(
        'layout_main' => 'layout',
        'left' => 'include/leftcp',
    );
    public $mytable = "tag";
    public $primaryKey = "TAG_ID";
    public $resCode = "TAG";

    function __construct() {
        parent::__construct();
    }

    function field() {
		$fieldData = array();
		$fieldData['TAG_NAME'] = '';
		return $fieldData;
    }
	
	function like($val = '', $param = null, $type='gets') {
        if ($param == null)
            $param = $this->primaryKey;
        if (is_array($val) && $val != array())
            $this->db->like($val);
        else if ($val != '')
            $this->db->like($param, $val);

        $this->db->from($this->mytable);
		if($type =='num')
			 return $this->db->count_all_results();
        return $this->db->get()->result();
    }
	
	/*
	$tags = array();
	$tags[] = array('name'=>'abc','link'=>'xyz');
	
	*/
	function tags_addContent($content, $tags){
		$tags = $this->tags_filter($tags);
		foreach($tags as $tag){
			$content= preg_replace('[(?!<[^>]*>)('.preg_quote($tag['name']).')(?![^<]*<\/) | (?!<[^>]*>)('.preg_quote($tag['name']).')(?![^<]*<\/)]',$tag['link'], $content,1);
		}
		return $content;
	}
	
	//remove same tag
	function tags_filter($tags = array()){
		$arr_data = array();
		for($i=0; $i<count($tags); $i++){
			//co danh dau la tim thay hay khong
			$flag = 0;
			for($j=0; $j<count($tags); $j++){
			if($i == $j)
				continue;
				//if tim thay
				if(stripos($tags[$j]['name'],$tags[$i]['name']) != false){
					$flag = 1;
					break;
				}
				else {
				}
			}

			if($flag ==0){
				$arr_data[] = $tags[$i];
			}
		}
		return $arr_data;
	}
}

?>